package com.lxk.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/4/24 11:23
 * @description 时间处理工具类
 */
public class CalendarUtil {

    /**
     * 最近时间类型 0全部 1近一年 2近半年(真实月份) 3近三个月（真实月份） 4本月（真实月份）
     */

    public static Date getStartDate(Integer type) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date date = null;
        if (type == 1) {
            calendar.add(Calendar.YEAR, -1);
            date = calendar.getTime();
        } else if (type == 2) {
            calendar.add(Calendar.MONTH, -5);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            date = calendar.getTime();
        } else if (type == 3) {
            calendar.add(Calendar.MONTH, -2);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            date = calendar.getTime();
        } else if (type == 4) {
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            date = calendar.getTime();
        } else {
            calendar.set(2000, 0, 1);
            date = calendar.getTime();
        }
        return date;
    }

    /**
     * 获取今日零点时间
     * @return
     */
    public static Date getTodayZero() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取今日起 n天的0点
     * @return
     */
    public static Date getSomedayZero(int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, num);
        return calendar.getTime();
    }

    /**
     * 获取近七日0点
     * @return
     */
    public static List<Date> getNearSevenZero() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        List<Date> list = new ArrayList<>();
        for (int i = 1; i <= 7; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            list.add(calendar.getTime());
        }
        return list;
    }

}
